package com.codename1.samples;


import com.codename1.components.SpanButton;
import com.codename1.components.SpanLabel;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import static com.codename1.ui.ComponentSelector.$;
import com.codename1.ui.Font;
import com.codename1.ui.FontImage;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class SpanLabelIconAlignmentTest {

    private Form current;
    private Resources theme;
    private static final String[] text = new String[]{
        "Connect with your clients",
        "View their activities and Saved searches",
        "One Dashboard for important updates on properties",
        "Receive feedback from clients on their favourite properties"
    };

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        
        Form hi = new Form("Hi World", BoxLayout.y());
        FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_FIBER_MANUAL_RECORD, new Label().getStyle(), 30);

        for (String str : text) {
            SpanLabel l = new SpanLabel(str);
            $("*", l).selectAllStyles().setFont(Font.createTrueTypeFont(Font.NATIVE_MAIN_REGULAR).derive(Display.getInstance().convertToPixels(4), Font.STYLE_PLAIN));
            l.setIcon(icon);
            hi.add(l);
        }
        for (String str : text) {
            SpanButton l = new SpanButton(str);
            l.setIcon(icon);
            hi.add(l);
        }
        
        icon = FontImage.createMaterial(FontImage.MATERIAL_FIBER_MANUAL_RECORD, new Label().getStyle(), 4);
        for (String str : text) {
            SpanLabel l = new SpanLabel(str);
            $("*", l).selectAllStyles()
                    //.setPaddingMillimeters(3)
                    .setFont(Font.createTrueTypeFont(Font.NATIVE_MAIN_REGULAR).derive(Display.getInstance().convertToPixels(4), Font.STYLE_PLAIN));
            l.setIcon(icon);
            hi.add(l);
        }
        for (String str : text) {
            SpanLabel l = new SpanLabel();
            l.setText(str); // set text after the fact to test different flow
            
            $("*", l).selectAllStyles()
                    //.setPaddingMillimeters(3)
                    .setFont(Font.createTrueTypeFont(Font.NATIVE_MAIN_REGULAR).derive(Display.getInstance().convertToPixels(4), Font.STYLE_PLAIN));
            l.setIcon(icon);
            hi.add(l);
        }
        hi.add(new Label("Hi World"));
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
